//
//  RHMyApprovalListController.m
//  RuiHeAppApp
//
//  Created by Jun on 2023/7/25.
//

#import "RHMyApprovalListController.h"
#import "RHMyApprovalListCell.h"
#import "RHExamineDetailVC.h"

@interface RHMyApprovalListController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic ,strong) UITableView *tableView;

@property(nonatomic ,strong)NSMutableArray *approvalListArr;
@end

@implementation RHMyApprovalListController
-(NSArray *)approvalListArr
{
    if(!_approvalListArr){
        _approvalListArr = [NSMutableArray array];
    }
    return _approvalListArr;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:CFTabViewBgColor];
    [self getMyApprovalList];
    [self setupUI];
}

//我的审核
-(void)getMyApprovalList
{
    [RHApplicationDetailModel startGetWithCompletionAPIKey:H_myApprovalList argument:@{@"hierarchyCode":@([PrivateMange share].currentUser.hierarchyCode)} requestSuccess:^(__kindof BaseRequest *request, ResultModel *resultModel) {
        NSArray *arr = resultModel.data;
        [self.approvalListArr removeAllObjects];
        for (RHApplicationDetailModel *model in arr) {
            if(self.index == 1){//待审核
                if(model.auditStatus == AuditStatusUnaudited){
                    [self.approvalListArr addObject:model];
                }
            }else if (self.index == 2){
                if(model.auditStatus == AuditStatusApproved||model.auditStatus == AuditStatusReject){
                    [self.approvalListArr addObject:model];
                }
            }else{
                self.approvalListArr = arr.mutableCopy;
            }
        }
        [self.tableView reloadData];
        [self.tableView endHeaderRefresh];
    } requestFail:^(__kindof BaseRequest *request, ResultModel *resultModel) {
        
    }];
}

- (void)setupUI{
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    [_tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [_tableView setBackgroundColor:UIColor.clearColor];
    [_tableView setDelegate:self];
    [_tableView setDataSource:self];
    [_tableView registerClass:[RHMyApprovalListCell class] forCellReuseIdentifier:@"RHMyApprovalListCell"];
    [self.view addSubview:_tableView];
    
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsZero);
    }];
    kWeakSelf;
    [_tableView addHeaderRefresh:^{
            [weakSelf getMyApprovalList];
    }];
}


#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.approvalListArr.count;;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.0;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return nil;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    RHMyApprovalListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RHMyApprovalListCell"];
    cell.model = self.approvalListArr[indexPath.row];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    RHExamineDetailVC *VC = [[RHExamineDetailVC alloc]init];
    RHApplicationDetailModel *model = self.approvalListArr[indexPath.row];
    if(model.auditStatus == AuditStatusUnaudited){
        VC.isAudit = YES;
    }
    VC.Id = model.audit_id;
    VC.title = @"Examine Detail";
    kWeakSelf;
    VC.finishBlock = ^{
        [weakSelf getMyApprovalList];
    };
    [self.navigationController pushViewController:VC animated:YES];
}

#pragma mark - JXCategoryListContentViewDelegate
/**
 实现 <JXCategoryListContentViewDelegate> 协议方法，返回该视图控制器所拥有的「视图」
 */
- (UIView *)listView {
    return self.view;
}

@end
